﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using 证件信息管理.Models;

namespace 证件信息管理.Controllers
{
    public class MyHomeController : Controller
    {
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string num, string pwd)
        {
            if (string.IsNullOrEmpty(num) || string.IsNullOrEmpty(pwd)) return Json(2);
            using (Model1 model = new Model1())
            {
                var info = model.UserInfo.Where(u => u.UserName == num).FirstOrDefault(u => u.Pwd == pwd);
                if (info != null)
                {
                    Session["UID"] = info.ID;
                    return Json(1);
                }
                else
                    return Json(0);
            }
        }

        [HttpGet]
        public ActionResult Index(string name, string isReview, string state)
        {
            Session["UID"] = 1;
            using (Model1 model = new Model1())
            {
                #region 载入所有数据
                var allINfo = model.Licence.Where(u => true).ToList();
                if (!string.IsNullOrEmpty(name))
                    allINfo = allINfo.Where(u => u.Name.IndexOf(name) >= 0).ToList();
                if (!string.IsNullOrEmpty(isReview))
                {
                    if (isReview == "1")
                        allINfo = allINfo.Where(u => u.IsReview == true).ToList();
                    else if (isReview == "0")
                        allINfo = allINfo.Where(u => u.IsReview == false).ToList();
                }
                if (!string.IsNullOrEmpty(state))
                    allINfo = allINfo.Where(u => u.State == state).ToList();
                #endregion

                #region 借出人姓名
                Dictionary<int, string> keyValuePairs = new Dictionary<int, string>();
                var nameList = model.UserInfo.Where(u => true).ToList();
                foreach (var item in nameList)
                    keyValuePairs.Add(item.ID, item.UserName);
                ViewBag.nameList = keyValuePairs;
                #endregion

                #region 年审下拉框
                List<SelectListItem> isReviewList = new List<SelectListItem>();
                isReviewList.Add(new SelectListItem() { Value = "", Text = "请选择" });
                isReviewList.Add(new SelectListItem() { Value = "0", Text = "不需要" });
                isReviewList.Add(new SelectListItem() { Value = "1", Text = "需要" });
                ViewBag.isReviewList = new SelectList(isReviewList, "Value", "Text");
                #endregion

                #region 状态下拉框
                List<SelectListItem> stateList = new List<SelectListItem>();
                stateList.Add(new SelectListItem() { Value = "", Text = "请选择" });
                stateList.Add(new SelectListItem() { Value = "借出", Text = "借出" });
                stateList.Add(new SelectListItem() { Value = "在库", Text = "在库" });
                ViewBag.stateList = new SelectList(stateList, "Value", "Text");
                #endregion

                ViewBag.State = Convert.ToInt32(Session["UID"]);

                return View(allINfo);
            }

        }

        [HttpGet]
        public ActionResult Create(string Length)
        {
            if (int.TryParse(Length, out int i))
                ViewBag.LengthIndex = false;

            using (Model1 model = new Model1())
            {
                #region 状态下拉框
                List<SelectListItem> stateList = new List<SelectListItem>();
                stateList.Add(new SelectListItem() { Value = "借出", Text = "借出" });
                stateList.Add(new SelectListItem() { Value = "在库", Text = "在库" });
                ViewBag.stateList = new SelectList(stateList, "Value", "Text");
                #endregion

                #region 借出人下拉框
                List<SelectListItem> perproList = new List<SelectListItem>();
                var nameList = model.UserInfo.Where(u => true).ToList();
                perproList.Add(new SelectListItem() { Value = "0", Text = "无" });
                foreach (var item in nameList)
                    perproList.Add(new SelectListItem() { Value = item.ID.ToString(), Text = item.UserName });
                ViewBag.perproList = new SelectList(perproList, "Value", "Text");
                #endregion

                return View(new Licence());
            }
        }

        [HttpPost]
        public ActionResult Create(Licence licence)
        {
            if (ModelState.IsValid)
            {
                if (!Convert.ToBoolean(licence.IsReview))
                    licence.ReviewDate = null;
                if (licence.State == "在库")
                    licence.LendUserID = null;
                else
                    if (licence.LendUserID == 0)
                    licence.LendUserID = null;

                using (Model1 model = new Model1())
                {
                    model.Licence.Add(licence);
                    if (model.SaveChanges() == 1)
                        return RedirectToAction("Create", "MyHome", "emmm");
                    else
                        return Json("添加失败");
                }
            }
            return Json("NULL");
        }

        [HttpPost]
        public ActionResult ClickUpdate(string id)//归还
        {
            if (int.TryParse(id, out int i))
            {
                var uId = Convert.ToInt32(id);
                using (Model1 model = new Model1())
                {
                    var info = model.Licence.FirstOrDefault(u => u.ID == uId);
                    info.State = "在库";
                    info.LendUserID = null;

                    return Json(model.SaveChanges());
                }
            }
            return Json(2);
        }

        [HttpPost]
        public ActionResult Update(string id)//借出
        {
            if (int.TryParse(id, out int i))
            {
                var uId = Convert.ToInt32(id);
                var userId = Convert.ToInt32(Session["UID"]);
                using (Model1 model = new Model1())
                {
                    var info = model.Licence.FirstOrDefault(u => u.ID == uId);
                    info.State = "借出";
                    info.LendUserID = userId;

                    return Json(model.SaveChanges());
                }
            }
            return Json(2);
        }

        [HttpPost]
        public ActionResult Delete(string id) {
            using (Model1 model = new Model1()) {
                var uId = Convert.ToInt32(id);
                var info = model.Licence.FirstOrDefault(u => u.ID == uId);
                model.Licence.Remove(info);
                return Json(model.SaveChanges());
            }
        }
    }
}